﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL;

namespace BuXiaChu.OrderDishes.Web.Admin.Modules.Sys
{
    public partial class SysUserList : System.Web.UI.UserControl
    {
        private List<ApplicationsModel> appList = null;
        private List<EmployeesModel> empList = null;
        private Dictionary<int, List<RolesModel>> usersRoleList = null;
        public List<SysUserInfoModel> List
        {
            get;
            set;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            appList = CodeDictionary.Instance.GetApplications();
            var empIds = "";
            var sysUserIds = "";
            List.ForEach(item =>
            {
                empIds += item.EmployeeId.ToString() + ",";
                sysUserIds += item.SystemUserId.ToString() + ",";
            });
            empIds = empIds.Trim(',');
            if (empIds.Length > 0)
            {
                empList = EmployeesManager.GetListByIds(empIds);
                sysUserIds = sysUserIds.Trim(',');
                usersRoleList = RolesManager.GetRoleListBySysUserIds(sysUserIds);
            }
        }

        protected string GetRoleNameBy(int userId)
        {
            var result = "";
            if (empList != null)
            {
                if (usersRoleList.ContainsKey(userId))
                {
                    result = usersRoleList[userId].Count > 0 ? usersRoleList[userId][0].Name : "";
                }
            }
            return result;
        }

        protected string GetLoginName(int empId)
        {
            var result = "";
            if (empList != null)
            {
                foreach (var item in empList)
                {
                    if (item.Id == empId)
                    {
                        result = item.Name;
                        break;
                    }
                }
            }
            return result;
        }

        protected string GetAppName(Guid id)
        {
            var name = "";
            foreach (var item in appList)
            {
                if (item.ApplicationId == id)
                {
                    name = item.Name;
                    break;
                }
            }
            return name;
        }
    }
}